<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片切换</title>
</head>
<body>
    <img src="images/1.png" id="flower">
    <br>
    <button id="prev">上一张</button>
    <button id="next">下一张</button>
    <script type="text/javascript">
        //获取事件源 需要的标签
        var flower = document.getElementById('flower');
        var nextBtn =document.getElementById('next');
        var prevBtn =document.getElementById('prev');
        //监听按钮的点击
        var minIndex=1,maxIndex=5;currentIndex=minIndex;
        nextBtn.onclick=function(){
            if(currentIndex===maxIndex){
                currentIndex=minIndex;
            }else{
                currentIndex++;
            }
            flower.setAttribute('src',`images/${currentIndex}.png`);
        }
        prevBtn.onclick=function(){
            if(currentIndex===minIndex){
                currentIndex=maxIndex;
            }else{
                currentIndex--;
            }
            flower.setAttribute('src',`images/${currentIndex}.png`);
        }
    </script>
</body>
</html>